package com.tanhua.admin.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author lifeihu
 * @date 2022/1/3
 * @TIME:18:48
 */
@Service
public class ManageService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private VideoApi videoApi;
    @DubboReference
    private MovementApi movementApi;
    @Autowired
    private RedisTemplate redisTemplate;
    public PageResult findUserByPage(Integer page, Integer pagesize) {
        IPage iPage = userInfoApi.findAll(page, pagesize);
        return PageResult.init(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }

    public UserInfo findUser(Long userId) {
        return userInfoApi.findById(userId);
    }

    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
       return videoApi.findByUserId(page, pagesize, uid);
    }
    //封装MovementsVo
    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
       PageResult pageResult=movementApi.findMovementsByUserid(uid,state,page,pagesize);
        List<Movement> items = (List<Movement>) pageResult.getItems();
        List<Long> ids = items.stream().map(Movement::getUserId).collect(Collectors.toList());
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids, null);
        ArrayList<MovementsVo> vos = new ArrayList<>();
        List<Movement> collect = items.stream().map(item -> {
            UserInfo userInfo = map.get(item.getUserId());
            if (userInfo != null) {
                MovementsVo vo = MovementsVo.init(userInfo, item);
                vos.add(vo);
            }
            return item;
        }).collect(Collectors.toList());
        pageResult.setItems(vos);
        return pageResult;
    }


    public Map userFreeze(Map params) {
        //1.获取用户id
        Long userId = Long.valueOf(params.get("userId").toString());
        //2.获取冻结时间
        String freezingTime1 = (String) params.get("freezingTime");
        Integer freezingTime = Integer.parseInt(freezingTime1);
        int day=0;
        if(freezingTime==1){
            day=3;
        }
        if(freezingTime==2){
            day=3;
        }
        if(freezingTime==3){
            day=-1;
        }
        String value = JSON.toJSONString(params);
        redisTemplate.opsForValue().set(Constants.FREEZE_USER+userId,value,day, TimeUnit.MINUTES);
        //查询用户
        UserInfo userInfo = userInfoApi.findById(userId);
        //修改用户状态
        userInfo.setUserStatus("2");
        userInfoApi.update(userInfo);
        //构造返回值
        HashMap map = new HashMap();
        map.put("message","冻结成功");
        return map;
    }

    public Map userUnfreeze(Map params) {
        Long userId = Long.valueOf(params.get("userId").toString());
        redisTemplate.delete(Constants.FREEZE_USER+userId);
        UserInfo userInfo = userInfoApi.findById(userId);
        userInfo.setUserStatus("1");
        userInfoApi.update(userInfo);
        Map map = new HashMap();
        map.put("message","解冻成功");
        return map;
    }
}
